package com.renjibo.teawithmilkgoods.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renjibo.teawithmilkgoods.R;
import com.renjibo.teawithmilkgoods.Model.GoodsBean;
import com.renjibo.teawithmilkgoods.ReFrecheRecycle;
import com.renjibo.teawithmilkgoods.adapters.HomeRecycleAdapter;
import com.renjibo.teawithmilkgoods.adapters.HomeRecycleRightAdapter;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment implements ReFrecheRecycle{
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private RecyclerView home_recycleView;
    private RecyclerView home_recycleView_right;
    private TextView home_textViewServer;
    private View backgroupView;
    private Gson gson;
    private List<GoodsBean.DataBean.CategoriesBean> categories;
    private GoodsBean goodsBean;
    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        gson = new Gson();
        postHttpJson();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);
       // TextView textView= (TextView) view.findViewById(R.id.home_textView);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
        home_recycleView.setLayoutManager(linearLayoutManager);

        //home_recycleView_right.
        LinearLayoutManager linearLayoutManager1=new LinearLayoutManager(getActivity());
        home_recycleView_right.setLayoutManager(linearLayoutManager1);

        postHttpJson();
        //textView.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );

        return view;
    }
public void initView(View view){
    home_recycleView = (RecyclerView) view.findViewById(R.id.home_recycleView);
    home_textViewServer = (TextView) view.findViewById(R.id.home_textViewServer);
    home_recycleView_right = (RecyclerView) view.findViewById(R.id.home_recycleView_right);
    home_recycleView.setItemAnimator(new DefaultItemAnimator());
    //添加分割线
 //   home_recycleView.addItemDecoration(new DividerItemDecoration(
   //         getActivity(), DividerItemDecoration.HORIZONTAL_LIST));
//    //添加分割线
//    home_recycleView.addItemDecoration(new DividerItemDecoration(
//            getActivity(), DividerItemDecoration.HORIZONTAL));
}
    //请求网络数据
    public void postHttpJson(){
        RequestParams params = new RequestParams("http://eleteamapi.ygcr8.com/v1/category/list-with-product");
        //params.addQueryStringParameter("wd", "xUtils");
        x.http().post(params, new Callback.CommonCallback<String>(){

            @Override
            public void onSuccess(String result) {
                goodsBean = gson.fromJson(result, GoodsBean.class);
                categories = goodsBean.getData().getCategories();
                for(GoodsBean.DataBean.CategoriesBean cb: categories){
                    Log.d("fragment","name:"+cb.getName());
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                home_textViewServer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                HomeRecycleAdapter homeRecycleAdapter=new HomeRecycleAdapter(goodsBean,getActivity());
                home_recycleView.setAdapter(homeRecycleAdapter);
                homeRecycleAdapter.setReFreach(HomeFragment.this);
                home_recycleView_right.setAdapter(new HomeRecycleRightAdapter(goodsBean.getData().getCategories().get(0),getActivity()));
            }
        });
    }

    @Override
    public void reFreach(int id,View v) {
        home_recycleView_right.setAdapter(new HomeRecycleRightAdapter(goodsBean.getData().getCategories().get(id),getActivity()));
        if(backgroupView!=null){
            backgroupView.setSelected(false);
        }
        backgroupView=v;
        v.setSelected(true);
    }
}
